package com.johnson.news.fragment;


import android.content.Intent;
import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import io.reactivex.functions.Consumer;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.androidkun.xtablayout.XTabLayout;
import com.google.android.material.tabs.TabLayout;
import com.johnson.news.R;
import com.johnson.news.activity.LoginActivity;
import com.johnson.news.activity.MyAskListActivity;
import com.johnson.news.adapter.CommonViewPagerAdapter;
import com.johnson.news.adapter.MyFragmentPagerAdapter;
import com.johnson.news.databinding.ViewAskmeSubmitBinding;
import com.johnson.news.event_msg.PostComplaintMsg;
import com.johnson.news.event_msg.SwitchAskMenuVisibleMsg;
import com.johnson.news.model.bean.BaseResponseData;
import com.johnson.news.model.bean.StatusItem;
import com.johnson.news.model.remote.RemoteDataSource;
import com.johnson.news.model.util.NewsPreference;
import com.johnson.news.util.Utils;
import com.youmbe.library.util.MyToast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link AskFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AskFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;


    public AskFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment AskFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static AskFragment newInstance(String param1, String param2) {
        AskFragment fragment = new AskFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }
    @Override
    public void onHiddenChanged(boolean hidden) {
        if(!hidden) {
            if(ask_viewpager.getCurrentItem() == 1 || ask_viewpager.getCurrentItem() == 2) {
                checkLogin();
            }
        }
    }

    final String query_url = "http://jyt.shaanxi.gov.cn/gzfw/zixunlist/index-htm-page-1-app-1.html";//http://jyt.shaanxi.gov.cn/gzfw/zixunlist/indexweixin-htm-page-1-app-1.html

    View rootView;
    XTabLayout ask_tab;
    ViewPager ask_viewpager;
    MyFragmentPagerAdapter adapter;
    ArrayList<String> titleList = new ArrayList<>();
    ArrayList<Fragment> viewList = new ArrayList<>();

    MyAskListFragment myAskListFragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if(rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_ask, container, false);
            EventBus.getDefault().register(this);
            init();
        }

        return rootView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    void init() {
        ask_tab = rootView.findViewById(R.id.ask_tab);
        ask_viewpager = rootView.findViewById(R.id.ask_viewpager);
        adapter = new MyFragmentPagerAdapter(getChildFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        ask_viewpager.setAdapter(adapter);

        ask_tab.setupWithViewPager(ask_viewpager);

        titleList.add(getString(R.string.ask_complaint_normal_question));
        titleList.add(getString(R.string.ask_complaint_title));
        titleList.add(getString(R.string.ask_complaint_of_my));

        myAskListFragment = new MyAskListFragment();
        viewList.add(new WebViewFragment(query_url));
        viewList.add(new PostComplaintFragment());
        viewList.add(myAskListFragment);

        adapter.setData(viewList, titleList);

        ask_viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if((position == 1 || position == 2) && !checkLogin()) {
                    return;
                }
//                if(position == 0) {
//                    EventBus.getDefault().post(new SwitchAskMenuVisibleMsg(false));
//                } else {
//                    EventBus.getDefault().post(new SwitchAskMenuVisibleMsg(true));
//                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    boolean checkLogin() {
        if(!NewsPreference.getinstance(getActivity()).getLoginState()) {
            ask_viewpager.setCurrentItem(0);
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
            MyToast.showToast("您还没有登录", getActivity());
            return false;
        }
        return true;
    }

    @Subscribe
    public void onPostComplaint(PostComplaintMsg postComplaintMsg) {
        if(myAskListFragment.hasData()) {
            ask_viewpager.setCurrentItem(2);
            myAskListFragment.refreshData();
        } else
            ask_viewpager.setCurrentItem(2);
    }


}
